classdef NMRPlotter < EquipmentControl.ZI.ziPlotter
    %NMRPLOTTER �˴���ʾ�йش����ժҪ
    %   �˴���ʾ��ϸ˵��
    
    properties
        channel129
        channel131
        centerFreq129
        centerFreq131
        gyro_offset
        
        fig
        ax1
        ax2

    end
    
    methods
        function obj = NMRPlotter(dev, session, varargin)
            %NMRPLOTTER ��������ʵ��
            %   �˴���ʾ��ϸ˵��
            obj@EquipmentControl.ZI.ziPlotter(dev, session);
            
        end
        
        function obj = setXe129Channel(obj, ch, freq)
            obj.channel129 = ch;
            obj.centerFreq129 = freq;
        end
        function obj = setXe131Channel(obj, ch, freq)
            obj.channel131 = ch;
            obj.centerFreq131 = freq;
        end
        
        function obj = setGyroOffset(obj, offset)
            obj.gyro_offset = offset;
        end
        
        function createMonitor(obj, varargin)
            p=inputParser;
            isnonneg = @(x) x>0;
            p.addParameter('window_time', 10.0, isnonneg);
            p.parse(varargin{:});
            
            obj.window_time = p.Results.window_time;
            
            obj.fig = figure;
            obj.ax1 = subplot(2, 1, 1);
            obj.ax2 = subplot(2, 1, 2);
            
            plot(obj.ax1, 0, obj.centerFreq129, 'ro-', 0, obj.centerFreq131, 'bd-');
            plot(obj.ax2, 0, 0.0 , 'k.-');
            obj.ax1.Children(1).Color=[1 0 0];
            obj.ax1.Children(2).Color=[0 0 1];

            title(obj.ax1,  sprintf('{\\color{red} f_{129} = %6.4f Hz}, {\\color{blue}f_{131} = %6.4f Hz}',...
                                    [obj.centerFreq129, obj.centerFreq131]));

            ylabel(obj.ax1, 'shift (mHz)');
            ylabel(obj.ax2, 'gyro signal (mHz)');
            
            xlim(obj.ax1, [-obj.window_time, 0]);
            xlim(obj.ax2, [-obj.window_time, 0]);
        end
    end
    
    methods
        function plotData(obj, ~, ~)
            freq129 = obj.getDataNode(obj.channel129, 'frequency', obj.data_polled);
            freq131 = obj.getDataNode(obj.channel131, 'frequency', obj.data_polled);
            
            tlist = freq129.time-freq129.time(end);
            idx = find(tlist>-obj.window_time, 1);
            
            g129=11.86; g131=3.516;
            try
                tlistGyro = tlist;
                gyro_sig = (g129*freq131.value - g131*freq129.value)./(g129+g131) - obj.gyro_offset;
            catch
                len = minmax([length(g129*freq131.value), length( g131*freq129.value)]);
                tlistGyro = tlist(1:len);
                gyro_sig = (g129*freq131.value(1:len(1)) - g131*freq129.value(1:len(1)) )./(g129+g131) - obj.gyro_offset;
                warning('Possible data loss %d', len(2)-len(1));
            end
            
            shift129 = (freq129.value(idx:end)-obj.centerFreq129)*1000.0; %mHz
            shift131 = (freq131.value(idx:end)-obj.centerFreq131)*1000.0; %mHz
            
            obj.ax1.Children(1).XData = tlist(idx:end); obj.ax1.Children(1).YData = shift129;
            obj.ax1.Children(2).XData = tlist(idx:end); obj.ax1.Children(2).YData = shift131;
            obj.ax2.Children(1).XData = tlistGyro(idx:end); obj.ax2.Children(1).YData = gyro_sig(idx:end) * 1000.0;
            
            drawnow;
        end
    end
end

